﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Caching;
using System.Text;

namespace WeChatParking.Core
{
    /// <summary>
    /// 配置项辅助操作类(Json.Config)
    /// </summary>
    public static class ConfigHelper
    {
        /// <summary>
        /// 获取所有配置值。并缓存在内存中
        /// </summary>
        /// <returns></returns>
        private static Dictionary<string, string> GetAllConfig()
        {
            try
            {
                ObjectCache cacheProvider = MemoryCache.Default;
                object data = cacheProvider.Get("WebJsonConfig");
                if (data != null)
                {
                    return (Dictionary<string, string>)data;
                }

                string configFileUrl = System.Web.HttpContext.Current.Server.MapPath("~/Web.Json.Config");
                string jsonStr = System.IO.File.ReadAllText(configFileUrl);
                Dictionary<string, string> allConfig = WeChatParking.Util.Conv.ParseJson<Dictionary<string, string>>(jsonStr);

                List<string> filePaths = new List<string>();
                filePaths.Add(configFileUrl);

                CacheItemPolicy policy = new CacheItemPolicy();
                policy.SlidingExpiration = new TimeSpan(0, 30, 0);
                policy.ChangeMonitors.Add(new HostFileChangeMonitor(filePaths));
                cacheProvider.Set("WebJsonConfig", allConfig, policy);

                return allConfig;
            }
            catch (System.IO.FileNotFoundException ex)
            {
                LogHelper.Error(ex);
                return new Dictionary<string, string>();
            }
            catch (Exception ex)
            {
                LogHelper.Error(ex);
                return new Dictionary<string, string>();
            }
        }

        /// <summary>
        /// 获取配置项值
        /// </summary>
        /// <param name="key">配置项键值。区分大小写</param>
        /// <returns></returns>
        public static string GetConfig(string key)
        {
            var dic = GetAllConfig();
            return dic.ContainsKey(key) ? dic[key] : string.Empty;
        }

        /// <summary>
        /// 获取配置项值
        /// </summary>
        /// <param name="item">指定配置项</param>
        /// <returns></returns>
        public static string GetConfig(ConfigItem item)
        {
            string key = item.ToString();
            return GetConfig(key);
        }

        /// <summary>
        /// 配置项枚举
        /// </summary>
        public enum ConfigItem
        {
            

            /// <summary>
            /// 支付宝合作身份者
            /// </summary>
            AlipayPartner,

            /// <summary>
            /// 支付宝收款账号
            /// </summary>
            AlipaySellerEmail,

            /// <summary>
            /// 支付宝交易安全检验码
            /// </summary>
            AlipayKey,

            /// <summary>
            /// 支付宝支付通知返回地址
            /// </summary>
            AlipayNotifyUrl,

            /// <summary>
            /// 支付宝移动支付通知返回地址
            /// </summary>
            AlipayMobileNotifyUrl,

            /// <summary>
            /// 支付宝支付通知返回地址
            /// </summary>
            AlipayReturnUrl,

            /// <summary>
            ///支付宝支付错误通知返回地址 
            /// </summary>
            AlipayErrorNotifyUrl,

            /// <summary>
            /// 移动端浏览器检测表达式，参考：http://detectmobilebrowsers.com/
            /// </summary>
            MobileClientUARegex_B,

            /// <summary>
            /// 移动端浏览器检测表达式，参考：http://detectmobilebrowsers.com/
            /// </summary>
            MobileClientUARegex_V
        }

    }

}
